import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/response/patrol/patrol_record_detail_data.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/patrol/patrol_detail_page.dart';

import '../../net/response/patrol/patrol_check_dats.dart';
import '../../viewmodel/patrol/patrol_list_view_model.dart';

class PatrolListFragment extends BaseStatefulMvvmWidget {
  final GuardTaskExecuteBeanList task;

  PatrolListFragment({required this.task});

  @override
  State<StatefulWidget> createState() => _PatrolListState();
}

class _PatrolListState
    extends BaseMvvmListState<PatrolListViewModel, PatrolListFragment> {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Column(
      children: [
        SizedBox(height: 16,),
        Row(
          children: [
            SizedBox(width: 16,),
            numberContainer("总点位数", "${widget.task.pointTotal}").setWeight(1),
            SizedBox(width: 12,),
            numberContainer("已巡点位数", "${widget.task.finishedNum}").setWeight(1),
            SizedBox(width: 12,),
            numberContainer("未巡点位数", "${widget.task.unfinishedNum}").setWeight(1),
            SizedBox(width: 16,),
            // CommonText("总点位数：${widget.task.pointTotal}",textAlign: TextAlign.center)
            //     .addToContainer(
            //   decoration: BoxDecoration(
            //     color: Colors.white,
            //     borderRadius: 5.borderRadius
            //   ),
            //   padding: EdgeInsets.symmetric(horizontal: 12,vertical: 12)
            // ).setWeight(1),
            // CommonText("已巡点位数：${widget.task.finishedNum}",textAlign: TextAlign.center).setWeight(1),
            // CommonText("未巡点位数：${widget.task.unfinishedNum}",textAlign: TextAlign.center).setWeight(1),
          ],
        ),
        loadingContentWidget.setWeight(1)
      ],
    );
  }

  Widget numberContainer(String title,String num){
    return Column(
      children: [
        CommonText(num,textSize: 17,fontWeight: FontWeight.bold),
        SizedBox(height: 6,),
        CommonText(title,textSize: 14,textColor: Color(0xff666666))
      ],
    ).addToContainer(
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: 5.borderRadius
        ),
        padding: EdgeInsets.symmetric(horizontal: 12,vertical: 8)
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Stack(
      alignment: Alignment.topCenter,
      children: [
        Positioned(
          child: Text(
            item.executeDate ?? "",
            textAlign: TextAlign.end,
            style: TextStyle(
              height: 1.2,
              fontSize: 15,
            ),
          ),
          left: 0,
          width: 160,
        ),
        Column(
          children: [
            Container(
              width: 1,
              color: Colours.app_main,
            ),
            (item.status == 1
                    ? Icon(
                        Icons.check,
                        size: 15,
                        color: Colours.app_main,
                      )
                    : Container())
                .addToContainer(
                    width: 20,
                    height: 20,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        border: Border.all(width: 2, color: Colours.app_main))),
            index == viewModel.listItems.length - 1
                ? Container()
                : Container(
                    width: 1,
                    color: Colours.app_main,
                    height: 50,
                  ),
          ],
        ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 20)),
        Positioned(
          right: 0,
          width: 160,
          child: Text(
            item.pointName??"",
            style: TextStyle(height: 1.2, fontSize: 15),
          ),
        )
      ],
    ).onTap(() async {
      if(item.resultStatus==0){
        var taskTime =  widget.task.endDate?.toDate();
        // DateTime? now =  await viewModel.getNetTime();
        if((DateTime.now()).isBefore(taskTime!)){
          showToast("该任务正常进行中，不可补卡");
          return;
        }
        
        var point = PatrolRecordDetailData.fromJson(item.toJson());
        point.startTime = widget.task.startDate;
        point.endTime = widget.task.endDate;
        point.patrolName = widget.task.name;
        point.patrolRange = widget.task.patrolRange;
        Go().push(PatrolDetailPage("",item: point,isResign: true,));
      }
    });
  }

  @override
  EdgeInsets get listPadding => EdgeInsets.only(top: 20);

  @override
  PatrolListViewModel createViewModel() {
    return PatrolListViewModel(context, widget.task);
  }
}
